package com.atguigu.gulimail.member.service.impl;

import com.atguigu.common.to.GiteeMemberVo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimail.member.dao.MemberDao;
import com.atguigu.gulimail.member.entity.MemberEntity;
import com.atguigu.gulimail.member.exception.ExistPhoneException;
import com.atguigu.gulimail.member.exception.ExistUsernameException;
import com.atguigu.gulimail.member.service.MemberLevelService;
import com.atguigu.gulimail.member.service.MemberService;
import com.atguigu.gulimail.member.vo.UserRegistVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {


    @Autowired
    MemberLevelService memberLevelService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveUser(UserRegistVo userRegistVo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberDao memberDao = this.baseMapper;


        ExistUsernameException(userRegistVo.getUsername());

        ExistPhoneException(userRegistVo.getPhone());


        memberEntity.setMobile(userRegistVo.getPhone());
        memberEntity.setUsername(userRegistVo.getUsername());

        Long defautLevelId = memberLevelService.selectDefautLevel();
        memberEntity.setLevelId(defautLevelId);


        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        memberEntity.setPassword(encoder.encode(userRegistVo.getPassword()));

        memberDao.insert(memberEntity);
    }

    @Override
    public void ExistUsernameException(String username) throws ExistUsernameException{

        Integer flag = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username",username));
        if(flag > 0){
            throw new ExistUsernameException();
        }

    }

    @Override
    public void ExistPhoneException(String phone) throws ExistPhoneException{
        Integer flag = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile",phone));
        if(flag > 0){
            throw new ExistPhoneException();
        }

    }

    @Override
    public MemberEntity GiteeSaveOrRegit(GiteeMemberVo giteeMemberVo) {

        String id = giteeMemberVo.getId();

        MemberDao baseMapper = this.baseMapper;
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", id));

        if(memberEntity != null){
            MemberEntity member = new MemberEntity();

            member.setId(memberEntity.getId());
            member.setExpires_in(memberEntity.getExpires_in());
            member.setAccessToken(memberEntity.getAccessToken());

            baseMapper.updateById(member);

            memberEntity.setAccessToken(giteeMemberVo.getAccess_token());
            memberEntity.setExpires_in(giteeMemberVo.getExpires_in());

            return memberEntity;
        }else {
            MemberEntity entity = new MemberEntity();

            entity.setSocialUid(giteeMemberVo.getId());
            entity.setAccessToken(giteeMemberVo.getAccess_token());
            entity.setExpires_in(giteeMemberVo.getExpires_in());
            entity.setNickname(giteeMemberVo.getName());
            entity.setEmail(giteeMemberVo.getEmaill());

            baseMapper.insert(entity);

            return entity;
        }

    }

}